package com.jamal2362.apktool.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.util.Colors;
import com.jamal2362.apktool.util.Settings;
import com.jamal2362.apktool.util.TextUtil;

public class MessageView extends ListView
{
	private LogAdapter adapter = null;
	private ArrayList<String> logarray = null;
	private Context mContext;
	private int textSize;

	public MessageView(Context context)
	{
		super(context);
		this.mContext = context;
		this.textSize = Settings.appFontSize - 2;
		this.logarray = new ArrayList<String>();
		this.adapter = new LogAdapter(context, R.id.logitemText, logarray);
		this.setAdapter(adapter);
		this.setDivider(null);
		this.setDividerHeight(0);
	}

	@Override
	public void setAdapter(ListAdapter adapter)
	{
		super.setAdapter(adapter);
	}

	public void textSize(int size)
	{
		this.textSize = size;
	}

	private int getLogColor(String s)
	{
		boolean lt = Settings.lightTheme;
		int color = (lt ? Colors.BLACK : Colors.WHITE);
		if (s.charAt(1) != ":".charAt(0))
			return color;
		switch (s.charAt(0))
		{
			case 'I':
				color = (lt ? Colors.VERY_DARK_GREEN : Colors.VERY_LIGHT_GREEN);
				break;

			case 'V':
				color = (lt ? Colors.BLACK : Colors.WHITE);
				break;

			case 'E':
				color = (lt ? Colors.DARK_RED : Colors.LIGHT_RED);
				break;

			case 'S':
				color = (lt ? Colors.VERY_DARK_RED : Colors.VERY_LIGHT_RED);
				break;

			case 'W':
				color = (lt ? Colors.DARK_BROWN : Colors.VERY_LIGHT_YELLOW);
				break;

			case 'D':
				color = (lt ? Colors.VERY_DARK_BLUE : Colors.VERY_LIGHT_BLUE);
				break;

			case 'A':
				color = Colors.PURPLE;
				break;
		}
		return color;
	}

	public void append(CharSequence s)
	{
		logarray.add(s.toString());
		setSelection(adapter.getCount() - 1);
	}

	public void append(ArrayList<String> list)
	{
		adapter = new LogAdapter(mContext, R.id.logitemText, list);
		setAdapter(adapter);
	}

	public CharSequence getText()
	{
		return listToString(logarray);
	}

	public ArrayList<String> getTextArray()
	{
		return logarray;
	}

	public String listToString(ArrayList<String> list)
	{
		String listString = "";
		for (String s : list)
		{
			listString += s + "\n";
		}
		return listString;
	}

	private class LogAdapter extends ArrayAdapter<String>
	{
		private List<String> objects = null;

		public LogAdapter(Context context, int textviewid, List<String> objects)
		{
			super(context, textviewid, objects);
			this.objects = objects;
		}

		@Override
		public int getCount()
		{
			return ((null != objects) ? objects.size() : 0);
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public String getItem(int position)
		{
			return ((null != objects) ? objects.get(position) : null);
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			View view = convertView;
			if (null == view)
			{
				LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = vi.inflate(R.layout.logview_item, null);
			}
			view.setMinimumHeight(0);
			String data = objects.get(position);
			if (null != data && !TextUtils.isEmpty(data))
			{
				final TextView textview = view.findViewById(R.id.logitemText);
				textview.setText(data);
				textview.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
				textview.setTextColor(getLogColor(data));
				textview.setOnClickListener(new OnClickListener()
					{
						public void onClick(View v)
						{
							TextUtil.setClipboard(mContext, textview.getText().toString());
						}
					});
			}
			return view;
		}
	}
}
